#################################################################################
### Title: Are People Willing to Trade Away Democracy for Desirable Outcomes? ###
### Authors: Jonathan A. Chu, Scott Williamson, Eddy S. F. Yeung              ###
### Content: Data cleaning for the Japanese sample                            ###
### Date: September 20, 2025                                                  ###
#################################################################################

### Set-up ----
## Clean the working environment and set the working directory
rm(list = ls())
setwd("~/Desktop/democracy_tradeoff/replication") # change to your own working directory

## Load the required packages
library(tidyverse)
library(estimatr)
library(cregg)
library(expss)
library(cjdata)

## Read the dataset
df <- read_Qualtrics("raw_data_JP.csv")

## Recode variable names for conjoint data
# Forced choice DV
df <- df %>% mutate(Q1.1 = case_when(
  is.na(`1_DV_tradeoff`) == FALSE ~ `1_DV_tradeoff`,
  is.na(`1_Q49`) == FALSE ~ `1_Q49`,
  is.na(`1_Q53`) == FALSE ~ `1_Q53`
))
table(df$Q1.1)

df <- df %>% mutate(Q1.4 = case_when(
  is.na(`2_DV_tradeoff`) == FALSE ~ `2_DV_tradeoff`,
  is.na(`2_Q49`) == FALSE ~ `2_Q49`,
  is.na(`2_Q53`) == FALSE ~ `2_Q53`
))
table(df$Q1.4)

df <- df %>% mutate(Q1.7 = case_when(
  is.na(`3_DV_tradeoff`) == FALSE ~ `3_DV_tradeoff`,
  is.na(`3_Q49`) == FALSE ~ `3_Q49`,
  is.na(`3_Q53`) == FALSE ~ `3_Q53`
))
table(df$Q1.7)

# Rating for Country A
df <- df %>% mutate(Q1.2 = case_when(
  is.na(`1_tradeoff_a`) == FALSE ~ `1_tradeoff_a`,
  is.na(`1_Q50`) == FALSE ~ `1_Q50`,
  is.na(`1_Q54`) == FALSE ~ `1_Q54`
))
table(df$Q1.2)

df <- df %>% mutate(Q1.5 = case_when(
  is.na(`2_tradeoff_a`) == FALSE ~ `2_tradeoff_a`,
  is.na(`2_Q50`) == FALSE ~ `2_Q50`,
  is.na(`2_Q54`) == FALSE ~ `2_Q54`
))
table(df$Q1.5)

df <- df %>% mutate(Q1.8 = case_when(
  is.na(`3_tradeoff_a`) == FALSE ~ `3_tradeoff_a`,
  is.na(`3_Q50`) == FALSE ~ `3_Q50`,
  is.na(`3_Q54`) == FALSE ~ `3_Q54`
))
table(df$Q1.8)

# Rating for Country B
df <- df %>% mutate(Q1.3 = case_when(
  is.na(`1_tradeoff_b`) == FALSE ~ `1_tradeoff_b`,
  is.na(`1_Q51`) == FALSE ~ `1_Q51`,
  is.na(`1_Q55`) == FALSE ~ `1_Q55`
))
table(df$Q1.3)

df <- df %>% mutate(Q1.6 = case_when(
  is.na(`2_tradeoff_b`) == FALSE ~ `2_tradeoff_b`,
  is.na(`2_Q51`) == FALSE ~ `2_Q51`,
  is.na(`2_Q55`) == FALSE ~ `2_Q55`
))
table(df$Q1.6)

df <- df %>% mutate(Q1.9 = case_when(
  is.na(`3_tradeoff_b`) == FALSE ~ `3_tradeoff_b`,
  is.na(`3_Q51`) == FALSE ~ `3_Q51`,
  is.na(`3_Q55`) == FALSE ~ `3_Q55`
))
table(df$Q1.9)

## Add attribute names
df$`F-1-1` <- df$`F-2-1` <- df$`F-3-1` <- "Leader Selection"
df$`F-1-2` <- df$`F-2-2` <- df$`F-3-2` <- "Civil Liberties"
df$`F-1-3` <- df$`F-2-3` <- df$`F-3-3` <- "Leader Constraints"
df$`F-1-4` <- df$`F-2-4` <- df$`F-3-4` <- "Corruption in Politics"
df$`F-1-5` <- df$`F-2-5` <- df$`F-3-5` <- "National Economy"
df$`F-1-6` <- df$`F-2-6` <- df$`F-3-6` <- "Respondent Wealth"
df$`F-1-7` <- df$`F-2-7` <- df$`F-3-7` <- "Public Safety"
df$`F-1-8` <- df$`F-2-8` <- df$`F-3-8` <- "Health Care"
df$`F-1-9` <- df$`F-2-9` <- df$`F-3-9` <- "Minority Treatment"
df$`F-1-10` <- df$`F-2-10` <- df$`F-3-10` <- "Respondent Identity"

## Recode variables of respondent characteristics
# Age (1 = older than 40, 0 = younger than 40)
df <- df %>% mutate(age_bin = case_when(
  age < 40 ~ 1,
  age > 40 ~ 0
))
df$age_bin <- factor(df$age_bin, 0:1, c("Older", "Younger Than 40"))
table(df$age_bin)

# Respondent Identity (1 = minority; 0 = otherwise)
df <- df %>% mutate(minority_bin = case_when(
  minority == "はい" ~ 1,
  minority == "いいえ" | minority == "わからない" ~ 0
))
df$minority_bin <- factor(df$minority_bin, 0:1, c("Non-Minority", "Minority"))
table(df$minority_bin)

# Gender (1 = female; 0 = male)
df <- df %>% rename(gender5 = gender)
df <- df %>% mutate(gender_bin = case_when(
  gender5 == "女性" ~ 1,
  gender5 == "男性" ~ 0
))
df$gender_bin <- factor(df$gender_bin, 0:1, c("Male", "Female"))
table(df$gender_bin)

# Self-reported political ideology (1 = right; 0 = left; NA = neither)
df <- df %>% mutate(ideo_bin = case_when(
  political == "非常に左翼" |
    political == "適度に左翼" |
    political == "やや左翼" ~ 0,
  political == "非常に右翼" |
    political == "適度に右翼" |
    political == "やや右翼" ~ 1
))
df$ideo_bin <- factor(df$ideo_bin, 0:1, c("Leftwing", "Rightwing"))
table(df$ideo_bin)

# Education (1 = college; 0 = no college)
df <- df %>% mutate(edu_bin = case_when(
  edu == "中学校卒業以下" |
    edu == "高校卒業または同程度の学歴" |
    edu == "短期大学士" |
    edu == "一部の大学、学位なし" ~ 0,
  edu == "学士号／4年制学士" |
    edu == "上級学位（修士号、博士号、法務博士など）" ~ 1
))
df$edu_bin <- factor(df$edu_bin, 0:1, c("No College", "College"))
table(df$edu_bin)

## Socioeconomic status (1 = high; 0 = low)
df <- df %>% mutate(SES = case_when(
  ladder == "1（最低）" |
    ladder == "2" | ladder == "3" | ladder == "4" | ladder == "5" ~ 0,
  ladder == "10（最高）" |
    ladder == "6" | ladder == "7" | ladder == "8" | ladder == "9" ~ 1
))
df$SES <- factor(df$SES, 0:1, c("Low SES", "High SES"))
table(df$SES)

## Reshape the dataset into wide format
df_cj <- reshape_conjoint(df, ResponseID, paste0("Q1.", seq(from = 1, to = 9, by = 3)))

## Reorder the factors
# Leader selection
df_cj$`Leader Selection` <- 
  factor(df_cj$`Leader Selection`,
         levels = c("軍事クーデター", "世襲", "選挙で選出されていない少数のエリート集団", "不当な選挙", "自由で公正な選挙"),
         labels = c("Military coup", "Hereditary succession", "Unelected elites", "Unfair elections", "Free and fair elections"))

# Civil liberties
df_cj$`Civil Liberties` <- 
  factor(df_cj$`Civil Liberties`,
         levels = c("自分の意見を表明したり、組織したりすると、必ず政府の厳しい抑圧に直面する", "自分の意見を表明したり、組織したりできるが、政府による抑圧のリスクに直面する", "自由に自分の意見を表明したり、組織したりできる"),
         labels = c("Repressed", "Partially free", "Free"))

# Leader constraints
df_cj$`Leader Constraints` <- 
  factor(df_cj$`Leader Constraints`,
         levels = c("ほとんどの場合、立法府と裁判所の権限を回避できる", "立法府と裁判所の権限を回避できる場合もある", "立法府と裁判所の権限を尊重しなければならない"),
         labels = c("Unconstrained", "Partially constrained", "Constrained"))

# Corruption in politics
df_cj$`Corruption in Politics` <- 
  factor(df_cj$`Corruption in Politics`,
         levels = c("いつも", "時々", "ごくまれ"),
         labels = c("Prevalent", "Occasional", "Rare"))

# National economy
df_cj$`National Economy` <- 
  factor(df_cj$`National Economy`,
         levels = c("低所得", "中所得", "高所得"),
         labels = c("Low income", "Middle income", "High income"))

# Respondent wealth
df_cj$`Respondent Wealth` <- 
  factor(df_cj$`Respondent Wealth`,
         levels = c("ほとんどの人より貧乏", "ほぼ平均", "ほとんどの人より裕福"),
         labels = c("Poor", "Average", "Wealthy"))

# Health care
df_cj$`Health Care` <- 
  factor(df_cj$`Health Care`,
         levels = c("お金やコネのある人たちだけがアクセスできる", "ほとんどの人たちがアクセスできる"),
         labels = c("For the privileged", "Mostly accessible"))

# Public safety
df_cj$`Public Safety` <- 
  factor(df_cj$`Public Safety`,
         levels = c("やや危険”, “非常に危険", "やや安全", "非常に安全"),
         labels = c("Very dangerous", "Somewhat safe", "Very safe"))

# Minority treatment
df_cj$`Minority Treatment` <- 
  factor(df_cj$`Minority Treatment`,
         levels = c("ほとんどの人たちから不当に扱われる", "公平に扱われる場合もあるが、不当に扱われる場合もある", "ほとんどの人たちから公平に扱われる"),
         labels = c("Mostly unfair", "Sometimes unfair", "Fairly treated"))

# Respondent identity
df_cj$`Respondent Identity` <- 
  factor(df_cj$`Respondent Identity`,
         levels = c("あなたは最小のマイノリティグループに属しています", "あなたは2番目に大きいグループに属しています", "あなたは最大のマジョリティグループに属しています"),
         labels = c("Minority", "Second largest", "Majority"))

### Save the cleaned dataset ----
## Incorporate covariates
df <- df %>% rename(id = ResponseId)
df_cj <- merge(df_cj, df, by = "id")

## Indicate the country
df_cj$country <- "JP"

## Export the dataset
write.csv(df_cj, "df_JP.csv", row.names = FALSE)
